﻿using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace AIPASoft.CommonLibrary.WPF.MVVM.ViewModel
{
    /// <summary>
    /// Implements INotifyPropertyChanged
    /// </summary>
    public class ViewModelBase : INotifyPropertyChanged
    {

        #region INotifyPropertyChanged Member

        /// <summary>
        /// PropertyChanged
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        #region Member

        /// <summary>
        /// Verifies the change in existing PropertyName
        /// </summary>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        /// <remarks>
        /// 1. Function Explanation
        /// VerifyPropertyName function
        ///     
        /// </remarks>
        public bool VerifyPropertyName(string propertyName)
        {
            return TypeDescriptor.GetProperties(this)[propertyName] != null;
        }

        /// <summary>
        /// Notifies the changes in Property
        /// CallerMemberName attribute is a new feature in c# 5.0
        /// </summary>
        /// <param name="propertyName"></param>
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            if (VerifyPropertyName(propertyName))
            {
                PropertyChangedEventHandler handler = PropertyChanged;
                if (handler != null)
                {
                    var e = new PropertyChangedEventArgs(propertyName);
                    handler(this, e);
                }
            }
        }


        #endregion
    }
}
